package com.example.shop.goods.dto;

import com.example.shop.common.bo.Page;
import com.example.shop.goods.enums.EvaluationSortRules;
import com.example.shop.goods.enums.EvaluationType;
import com.example.shop.goods.enums.SortOrder;
import lombok.Data;
import lombok.Getter;

import javax.validation.constraints.NotNull;

@Data
public class EvaluationQueryParam {
    public static final String QUERY_SQL =
            " " +
            "<choose>" +
                "<when test = 'ew != null'>" +
                    "<if test = 'ew.goodsId != null'>" +
                        "e.goods_id = ${ew.goodsId} and " +
                    "</if>" +
                    "<if test = 'ew.userPurchasedGoodsClass != null'>" +
                        "user_purchased_goods_class = #{ew.userPurchasedGoodsClass} and " +
                    "</if>" +
                    "<if test = 'ew.evaluationType != null'>" +
                        "e.evaluation_type = #{ew.evaluationType} and " +
                    "</if>" +
                    " 1 " +
                    "ORDER BY "+EvaluationSortRules.SORT_SQL+ SortOrder.SORT_WAY_SQL+
                    "<if test='ew.page != null'>" +
                        "limit ${(ew.page.pageIndex-1)*ew.page.pageSize},${ew.page.pageSize}"+
                    "</if>"+
                "</when>" +
                "<otherwise>" +
                " 1 "+
                "</otherwise>" +
            "</choose>";
    @NotNull
    private Long goodsId;
    private String userPurchasedGoodsClass;
    private EvaluationType evaluationType;
    //排序规则
    private EvaluationSortRules sortRules;
    //排序顺序
    protected SortOrder sortOrder = SortOrder.ASC;
    //分页
    protected Page page = new Page(1);



    private EvaluationQueryParam(){
    }
    public static Builder builder(){
        return new Builder();
    }

    public static class Builder{
        private EvaluationQueryParam evaluationQueryParam;
        public Builder(){
            evaluationQueryParam = new EvaluationQueryParam();
        }

        public EvaluationQueryParam build(){
            return evaluationQueryParam;
        }

        public Builder setGoodsId(Long goodsId) {
            evaluationQueryParam.goodsId = goodsId;
            return this;
        }

        public Builder setUserPurchasedGoodsClass(String userPurchasedGoodsClass) {
            evaluationQueryParam.userPurchasedGoodsClass = userPurchasedGoodsClass;
            return this;
        }

        public Builder setEvaluationType(EvaluationType evaluationType) {
            evaluationQueryParam.evaluationType = evaluationType;
            return this;
        }

        public Builder setSortRules(EvaluationSortRules sortRules) {
            evaluationQueryParam.sortRules = sortRules;
            return this;
        }

        public Builder setSortOrder(SortOrder sortOrder) {
            evaluationQueryParam.sortOrder = sortOrder;
            return this;
        }
        public Builder setPage(Page page) {
            evaluationQueryParam.page = page;
            return this;
        }

    }
}
